
package UTIL;

import datechooser.beans.DateChooserCombo;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * The InputValidator servers as a tool check user Input depending on the 
 * conditions set.
 * @author Belthazod
 */
public class InputValidator {
    
/**
 *  Checks if the given String is Empty. If so, a Dialog box appears
 *  indicating the error message specified with it.
 * 
 * @param input - the String to be checked
 * @param message - the Error message to show when the String is empty.
 * @return result - true if the given String is not empty otherwise returns 
 *      false.
 */
    public static boolean checkInput(String input, String message){
        boolean result = true;
        try{
            if(input.trim().equals("")){
            JOptionPane.showMessageDialog(null, message, "Input error", JOptionPane.ERROR_MESSAGE);
            result = false;
            }
        }catch(NullPointerException npe){
            result = false;
        }
        return result;
    }

/**
 *  Clears a given set of JTextFields of any text.
 * 
 * @param inputs - the array of JTextFields to be cleared of any value.
 */
    public static void clearInput(JTextField[] inputs){
        for(int i = 0; i<inputs.length ; i++){
            inputs[i].setText("");
        }
    }

/**
 *  Clears a single JTextField of any text.
 * 
 * @param input - the JTextField to be cleared of any value.
 */
    public static void clearInput(JTextField input){
        input.setText("");
    }
    
    /**
 *  Clears a given set of JLabels of any text.
 * 
 * @param labels - the array of JLabels to be cleared of any value.
 */
    
    
    public static void clearInput(JLabel[] labels){
        for(int i = 0; i<labels.length ; i++){
            labels[i].setText("");
        }
    }

/**
 *  Clears a single JTextField of any text.
 * 
 * @param label - the JTextField to be cleared of any value.
 */
    public static void clearInput(JLabel label){
        label.setText("");
    }
    
/**
 *  Checks if the given String is of a valid phone number
 * 
 * @param input - the String to be checked.
 */
    
    public static String dateFormat(DateChooserCombo date){
        String dDate = "";
        try{
        DateFormat dfto = new SimpleDateFormat("yyyy-MM-dd");
        String dat  = date.getText();
        //System.out.println(dat);
        String[] deyt = dat.split(" ", 4);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new SimpleDateFormat("MMM").parse(deyt[1]));
        int monthInt = cal.get(Calendar.MONTH) + 1;
        String deyt2 = deyt[2];
        deyt2 = deyt2.replace(",", "");
        dDate = deyt[3] + "-" + monthInt + "-" + deyt2;
        //System.out.println(a);                
        }catch(Exception e){
            e.printStackTrace();
        }
        return dDate;
    }
       
    public static boolean checkIfNumber(String input, String message){
        try{
            Integer.parseInt(input);
            return true;
        }catch(NumberFormatException x){
            JOptionPane.showMessageDialog(null, message, "Input error", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
     public static void enabler(JTable table, JButton button){
        
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
        public void valueChanged(ListSelectionEvent event) {
            ListSelectionModel lsm = (ListSelectionModel)event.getSource();
            if(!lsm.isSelectionEmpty()){
            button.setEnabled(true);
            }else{
                button.setEnabled(false);
            }
        }
    });
    }
     
     public static void enabler2(JTable table, JButton button){
         int rows = table.getRowCount();
         if(rows != 0){
             button.setEnabled(true);
         }else{
             button.setEnabled(false);
         }
     }
     

}